package cn.iocoder.yudao.module.wms.controller.admin.driver;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.driver.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.driver.DriverDO;
import cn.iocoder.yudao.module.wms.service.driver.DriverService;

@Tag(name = "管理后台 - 驾驶员资料")
@RestController
@RequestMapping("/wms/driver")
@Validated
public class DriverController {

    @Resource
    private DriverService driverService;

    @PostMapping("/create")
    @Operation(summary = "创建驾驶员资料")
    @PreAuthorize("@ss.hasPermission('wms:driver:create')")
    public CommonResult<String> createDriver(@Valid @RequestBody DriverSaveReqVO reqVO) {
        return success(driverService.createDriver(reqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新驾驶员资料")
    @PreAuthorize("@ss.hasPermission('wms:driver:update')")
    public CommonResult<Boolean> updateDriver(@Valid @RequestBody DriverSaveReqVO reqVO) {
        driverService.updateDriver(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除驾驶员资料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:driver:delete')")
    public CommonResult<Boolean> deleteDriver(@RequestParam("id") String id) {
        driverService.deleteDriver(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得驾驶员资料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:driver:query')")
    public CommonResult<DriverRespVO> getDriver(@RequestParam("id") String id) {
        DriverDO driver = driverService.getDriver(id);
        return success(BeanUtils.toBean(driver, DriverRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得驾驶员资料分页")
    @PreAuthorize("@ss.hasPermission('wms:driver:query')")
    public CommonResult<PageResult<DriverRespVO>> getDriverPage(@Valid DriverPageReqVO pageReqVO) {
        PageResult<DriverDO> pageResult = driverService.getDriverPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DriverRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出驾驶员资料 Excel")
    @PreAuthorize("@ss.hasPermission('wms:driver:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDriverExcel(@Valid DriverPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DriverDO> list = driverService.getDriverPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "驾驶员资料.xls", "数据", DriverRespVO.class,
                        BeanUtils.toBean(list, DriverRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得驾驶员资料列表（无分页）")
    @PreAuthorize("@ss.hasPermission('wms:driver:query')")
    public CommonResult<List<DriverRespVO>> getDriverList() {
        // 大数据量慎重
        List<DriverDO> list = driverService.list();
        return success(BeanUtils.toBean(list, DriverRespVO.class));
    }
}